import { _decorator, Color, Component, instantiate, Label, Node, Prefab, ScrollView, Sprite, SpriteFrame } from 'cc';
import { UIManager } from '../common/UIManager';
import { UserInfo } from '../common/UserInfo';
import { Building } from '../entity/Buildings';
import ResManager from '../common/ResManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { BuildingItem } from './BuildingItem';

import { EventManager } from '../event/EventManager';
import { VillageController } from './VillageController';
import { UIHeader } from '../common/UIHeader';
import { TabLayout } from '../common/TabLayout';
import { BManager } from '../entity/BManager';
import { Space } from './Space';
const { ccclass, property } = _decorator;

/**视图显示类型 */
enum ViewType {
    MyBuildings,
    NewBuildings,
    MyRes,
    King
}

@ccclass('BuildingList')
export class BuildingList extends Component {
    public static instance: BuildingList;
    @property(UIHeader)
    public uiHeader:UIHeader
    @property(TabLayout)
    public tabLayout:TabLayout
   
    @property(ScrollView)
    public scrollView:ScrollView
    @property(Node)
    public content:Node;

    // @property(Node)
    // public myBuildingsBtn:Node;

    // @property(Node)
    // public newBuildingsBtn:Node;
    // @property(Node)
    // public myResBtn:Node;
    // @property(Node)
    // public kingBtn:Node;
    // @property(SpriteFrame)
    // public btn1:SpriteFrame
    // @property(SpriteFrame)
    // public btn2:SpriteFrame

    public buildings:BManager[] = []

    private viewType:ViewType

    protected onLoad(): void {
        BuildingList.instance = this;
    }

    start() {
        this.buildings = UserInfo.instance.villageData.bManager.bList
        

        this.uiHeader.setData('建造')
        EventManager.instance.on('建造close', this.onClose, this)
        this.tabLayout.setData(['我的建筑','新建建筑','我的资源田'])
        EventManager.instance.on('我的建筑',this.createMyBuildings, this )
        EventManager.instance.on('新建建筑',this.createNewBuildings, this )
        EventManager.instance.on('我的资源田',this.createMyRes, this )
        // EventManager.instance.on('君主天赋',this.createKing, this )

        this.tabLayout.setActiveByName('新建建筑')
        this.createNewBuildings()
    }

    protected onDisable(): void {
       
    }

    /**刷新视图 */
    refreshView(){
        if(this.viewType == ViewType.MyBuildings){
            this.createMyBuildings()
        }
        if(this.viewType == ViewType.NewBuildings){
            this.createNewBuildings()
        }
        if(this.viewType == ViewType.MyRes){
            this.createMyRes()
        }
        if(this.viewType == ViewType.King){
            this.createKing()
        }
    }

  
    /**构建“新建建筑”的视图 */
    async createNewBuildings(){
        await UIManager.instance.showLoading()
        this.content.removeAllChildren()
     
       
        this.viewType = ViewType.NewBuildings
        // await this.getWorker(false)
        // await this.getTitle('请选择建筑')
        await this.getBuildings()
        this.scrollView.scrollToTop()
        UIManager.instance.closeLoading()
    }
    /**构建“我的建筑”的视图 */
    async createMyBuildings(){  
        
        await UIManager.instance.showLoading()
        this.content.removeAllChildren()
       
        this.viewType = ViewType.MyBuildings
        // await this.getWorker(true)
        // await this.getTitle('我的建筑')
        await this.getMyBuildins()
        this.scrollView.scrollToTop()
        UIManager.instance.closeLoading()
    }
    /**构建“我的资源田”的视图 */
    async createMyRes (){
        await UIManager.instance.showLoading()
        this.content.removeAllChildren()
        
        this.viewType = ViewType.MyRes
        // await this.getWorker(false)
        // await this.getTitle('我的资源田')
        await this.getMyRes()
        this.scrollView.scrollToTop()
        UIManager.instance.closeLoading()
    }
    /**构建“君主天赋”的视图 */
    async createKing(){
        await UIManager.instance.showLoading()
        this.content.removeAllChildren()
       
        this.viewType = ViewType.King
        this.scrollView.scrollToTop()
        UIManager.instance.closeLoading()
    }
    /**
     * 获取工人
     * @param isNeedRemoveWorker 是否需要拆除工人
     */
    async getWorker(isNeedRemoveWorker:boolean){
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.BuildingItem)

        let innerWorker = UserInfo.instance.getInnerWorker()
        let outerWorker = UserInfo.instance.getOuterWorker()
        let idleWorker = UserInfo.instance.getIdleWorker()
        let removeWorker = UserInfo.instance.getRemoveWorker()
        let idleRemoveWorker = UserInfo.instance.getIdleRemoveWorker()
        
        let list = [
           
        ]

        if(isNeedRemoveWorker){
            list = [
                ...innerWorker,
                ...outerWorker,
                ...idleWorker,
                ...removeWorker,
                ...idleRemoveWorker
            ]
        } else {
            list = [
                ...innerWorker,
                ...outerWorker,
                ...idleWorker,
            ]
        }

        for(let item of list){
            let gameObject = instantiate(prefab)
            this.content.addChild(gameObject)
            gameObject.getComponent(BuildingItem).setWorker(item)
        }
    }

    /**
     * 获取标题
     * @param str 
     */
    async getTitle(str:string){
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.SelectBuildTitle)
        let gameObject = instantiate(prefab)
        this.content.addChild(gameObject)
        gameObject.getChildByName('Label').getComponent(Label).string = str

    }  
    /**
     * 获取可建造的建筑物
     */
    async getBuildings(){
        let list = Building.createAllBuilding()
        
        list = list.slice(5)
        console.log(list)
        list = list.filter((item) => {
            return item.isCanBuild
        })
        console.log(list)
        
        // list.sort((a, b) => {
        //     return a.lackLevel.length - b.lackLevel.length
        // })


        if (VillageController.instance.curClickSpace) {
            
            let space = VillageController.instance.curClickSpace.getComponent(Space)
            list = list.filter((item) => {
                return space.buildData.canBuildOnSpace(item)
            })
            
        }
       
        
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.BuildingItem)
        for(let item of list){
            let gameObject = instantiate(prefab)
            this.content.addChild(gameObject)

            let com = gameObject.getComponent(BuildingItem)
            com.setData(item)
            com.checkIsCanBuild()
        }
    }

    /**
     * 获取我的建筑物
     */
    async getMyBuildins(){
        let list = Building.getMyBuildings()
        
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.BuildingItem)
        for(let item of list){
            let gameObject = instantiate(prefab)
            this.content.addChild(gameObject)

            gameObject.getComponent(BuildingItem).setData(item)
        }
    }
    /**
     * 获取我的资源田
     */
    async getMyRes(){
        let list = Building.getMyRes()
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.BuildingItem)
        for(let item of list){
            let gameObject = instantiate(prefab)
            this.content.addChild(gameObject)

            gameObject.getComponent(BuildingItem).setData(item)
        }
    }

    onClose(){
        UIManager.instance.closeUI(PrefabEnum.BuildingList)

        VillageController.instance.closeSpaceTip()
    }


 
}


